package com.example.wuqujingdianphone.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.api.GlobalConfig;
import com.example.wuqujingdianphone.base.BaseActivity;
import com.example.wuqujingdianphone.bean.LoginBean;
import com.example.wuqujingdianphone.utils.Message1Event;
import com.example.wuqujingdianphone.utils.SPUtil;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PassWordModifyActivity extends BaseActivity {
    private static final String TAG = "PassWordModifyActivity";
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.oldPassword)
    EditText mOldPassword;
    @BindView(R.id.newPassword)
    EditText mNewPassword;
    @BindView(R.id.commitPassword)
    EditText mCommitPassword;
    @BindView(R.id.ok)
    Button mOk;
    private String accout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pass_word_modify);
        ButterKnife.bind(this);
        mTitle.setText("密码修改");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        accout = SPUtil.getString(PassWordModifyActivity.this, GlobalConfig.ACCOUNT_KEY, "");
        Log.i(TAG, "onCreate: accout=" + accout);


        //http://wenqujingdian.com/index.php/Jiekou/savepass
        mOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLegal()) {
                    ViseHttp.GET("index.php/Jiekou/savepass")
                            .tag(TAG)
                            .addParam("oldpass", strOldPassword)
                            .addParam("password", strNewPassword)
                            .addParam("username", accout)
                            .request(new ACallback<String>() {
                                @Override
                                public void onSuccess(String result) {
                                    Log.i(TAG, "onSuccess: result=" + result);
                                    SPUtil.putString(PassWordModifyActivity.this, "password", "");
                                    SPUtil.putBoolean(PassWordModifyActivity.this, "isLogin", false);
                                    EventBus.getDefault().post(new Message1Event(3, GlobalConfig.LOG_OUT));
                                    ToastUtil.show("修改成功");
                                    finish();


                                }

                                @Override
                                public void onFail(int errCode, String errMsg) {
                                    //请求失败，errCode为错误码，errMsg为错误描述
                                    ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                                }
                            });
                }


            }
        });

    }

    private String strOldPassword;
    private String strNewPassword;
    private String strCommitPassword;


    public boolean isLegal() {
        strOldPassword = mOldPassword.getText().toString().trim();
        strNewPassword = mNewPassword.getText().toString().trim();
        strCommitPassword = mCommitPassword.getText().toString().trim();

        if (TextUtils.isEmpty(strOldPassword)) {
            ToastUtil.show("原始密码不能为空");
            return false;

        }

        if (TextUtils.isEmpty(strNewPassword)) {
            ToastUtil.show("新密码不能为空");
            return false;

        }

        if (TextUtils.isEmpty(strCommitPassword)) {
            ToastUtil.show("确认密码不能为空");
            return false;
        }

        if (!strNewPassword.equals(strCommitPassword)) {
            ToastUtil.show("两次密码不一样");
            return false;
        }


        return true;
    }


}
